package org.kordakus.shoot.gui;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.HashSet;
import java.util.Set;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

import org.joda.time.DateTime;
import org.kordakus.shoot.ShootContext;
import org.kordakus.shoot.model.ImageFile;

public class ImageTableModel implements TableModel, PropertyChangeListener
{

	private final ShootContext				_context;

	private final Set<TableModelListener>	_listeners	= new HashSet<TableModelListener>();

	public ImageTableModel(final ShootContext context)
	{
		this._context = context;
		initializeObject();
	}

	private void initializeObject()
	{
		_context.addPropertyListener(ShootContext.PROPERTY_IMAGE_LIST, this);
	}

	@Override
	public int getRowCount()
	{
		return _context.getImageCount();
	}

	@Override
	public int getColumnCount()
	{
		return 5;
	}

	@Override
	public String getColumnName(int columnIndex)
	{
		switch (columnIndex)
		{
			case ShootGuiUtils.TABLE_FILENAME_INDEX:
				return "Image";
			case ShootGuiUtils.TABLE_CAMERAMAKE_INDEX:
				return "Camera make";
			case ShootGuiUtils.TABLE_CAMERAMODEL_INDEX:
				return "Camera model";
			case ShootGuiUtils.TABLE_DATETIMEORIGINAL_INDEX:
				return "Date taken";
			case ShootGuiUtils.TABLE_NEWDATETIMEORIGINAL_INDEX:
				return "Updated date taken";
			default:
				return "";
		}
	}

	@Override
	public Class<?> getColumnClass(int columnIndex)
	{
		switch (columnIndex)
		{
			case ShootGuiUtils.TABLE_FILENAME_INDEX:
				return File.class;
			case ShootGuiUtils.TABLE_CAMERAMAKE_INDEX:
			case ShootGuiUtils.TABLE_CAMERAMODEL_INDEX:
				return String.class;
			case ShootGuiUtils.TABLE_DATETIMEORIGINAL_INDEX:
			case ShootGuiUtils.TABLE_NEWDATETIMEORIGINAL_INDEX:
				return DateTime.class;
			default:
				return String.class;
		}
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex)
	{
		switch (columnIndex)
		{
			case ShootGuiUtils.TABLE_FILENAME_INDEX:
			case ShootGuiUtils.TABLE_CAMERAMAKE_INDEX:
			case ShootGuiUtils.TABLE_CAMERAMODEL_INDEX:
			case ShootGuiUtils.TABLE_DATETIMEORIGINAL_INDEX:
				return false;
			case ShootGuiUtils.TABLE_NEWDATETIMEORIGINAL_INDEX:
				return true;
			default:
				return false;
		}
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex)
	{
		ImageFile image = _context.getImages().get(rowIndex);
		switch (columnIndex)
		{
			case ShootGuiUtils.TABLE_FILENAME_INDEX:
				return image.getFile();
			case ShootGuiUtils.TABLE_CAMERAMODEL_INDEX:
				return image.getCameraModel();
			case ShootGuiUtils.TABLE_CAMERAMAKE_INDEX:
				return image.getCameraMake();
			case ShootGuiUtils.TABLE_DATETIMEORIGINAL_INDEX:
				return image.getDateCreated();
			case ShootGuiUtils.TABLE_NEWDATETIMEORIGINAL_INDEX:
				return _context.getDateTime(image);
			default:
				return "";
		}
	}

	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex)
	{
	}

	@Override
	public void addTableModelListener(TableModelListener l)
	{
		if (l != null)
		{
			_listeners.add(l);
		}
	}

	@Override
	public void removeTableModelListener(TableModelListener l)
	{
		_listeners.remove(l);
	}

	//
	//
	// Property Change Listener interface

	@Override
	public void propertyChange(PropertyChangeEvent evt)
	{
		TableModelEvent event = new TableModelEvent(this);
		for (TableModelListener listener : _listeners)
		{
			listener.tableChanged(event);
		}
	}

}
